//替换参数
function replaceParam(key, value) {
    var filter_path, filter_arr, filter_result, filter_len, params;
    filter_path = window.location.pathname;
    filter_len = filter_path.match(/\//g).length;
    if (filter_len && filter_len >= 2) {
        filter_path = filter_path.replace('.html', '');
        filter_arr = filter_path.split('/');
        for (var j = 0; j < filter_arr.length; j++) {
            if(j === 0){
                filter_result = 'app=';
            }else{
                filter_result += j%2 ? filter_arr[j] : '&'+filter_arr[j]+'=';
            }
        }
        params = filter_result.split('&');
    } else {
        params = location.search.substring(1).split('&');
    }
    var found = false;
    var key, param, arr, pKey;
    for (var i = 0; i < params.length; i++) {
        param = params[i];
        arr = param.split('=');
        pKey = arr[0];
        if (pKey === 'page') {
            params.splice(i, 1);
        }
        if (pKey === key) {
            params[i] = key + '=' + value;
            found = true;
        }
    }
    if (!found) {
        value = transform_char(value);
        params.push(key + '=' + value);
    }
    
    location.assign(SITE_URL + '/index.php?' + params.join('&'));
    
//    if(filter_path.indexOf('index.php') === -1 && params[0].indexOf('search') === -1){
//        location.assign(SITE_URL + '/' + Rewrite.temp_get(params.join('&')));
//    }else{
//        location.assign(SITE_URL + '/index.php?' + params.join('&'));
//    }
    
    window.event.returnValue = false;
}

//删除参数
function dropParam(key) {
    var filter_path, filter_arr, filter_result, filter_len, params;
    filter_path = window.location.pathname;
    filter_len = filter_path.match(/\//g).length;
    if (filter_len && filter_len >= 2) {
        filter_path = filter_path.replace('.html', '');
        filter_arr = filter_path.split('/');
        for (var j = 0; j < filter_arr.length; j++) {
            if(j === 0){
                filter_result = 'app=';
            }else{
                filter_result += j%2 ? filter_arr[j] : '&'+filter_arr[j]+'=';
            }
        }
        params = filter_result.split('&');
    } else {
        params = location.search.substring(1).split('&');
    }
    var key, param, arr, pKey;
    for (var i = 0; i < params.length; i++) {
        param = params[i];
        arr = param.split('=');
        pKey = arr[0];
        if (pKey == key) {
            params.splice(i, 1);
        }
        if (pKey == 'page') {
            params.splice(i, 1);
        }
    }
    location.assign(SITE_URL + '/index.php?' + params.join('&'));
//    if(filter_path.indexOf('index.php') === -1 && params[0].indexOf('search') === -1){
//        location.assign(SITE_URL + '/' + Rewrite.temp_get(params.join('&')));
//    }else{
//        location.assign(SITE_URL + '/index.php?' + params.join('&'));
//    }
    window.event.returnValue = false;
}

//清空参数
function emptyParam() {
    var li_filters = $('#location > li[ectype="li_filter"]');
    var params = location.search.substr(1).split('&');
    var key, param, arr, pKey;
    li_filters.each(function() {
        key = $(this).children('a').attr('ecvalue');
        for (var i = 0; i < params.length; i++) {
            param = params[i];
            arr = param.split('=');
            pKey = arr[0];
            if (pKey == key) {
                params.splice(i, 1);
            }
        }
    });
    location.assign(SITE_URL + '/index.php?' + params.join('&'));
    window.event.returnValue = false;
}

//关键字查询
function get_keywords() {
    var value = $.trim($('#location_input').val());
    var key = 'keyword';
    if (value == '在此条件下查找') {
        return;
    }
    replaceParam(key, value);
}

//获取价格
function get_price() {
    var start_price = $.trim($('#start_price').val());
    var end_price = $.trim($('#end_price').val());
    var value = start_price + '-' + end_price;
    var key = 'price';
    replaceParam(key, value);
}

//获取起订量
function get_start_num() {
    var value = $.trim($('#start_num').val());
    if(value === '')
        value = '0';
    var key = 'start_stock';
    replaceParam(key, value);
}

$(function() {
    var selectwrap = $('#selectwrap'),
        location = $('#location'),
        li_filters = $('#location > li[ectype="li_filter"]');
    if (li_filters.length > 0) {
        li_filters.each(function() {
            var attr_id = $(this).children('a').attr('attr_id');
            var dl = $('#attr_id_' + attr_id);
            dl.hide();
        });
    }
    // 属性切换
    if (selectwrap.length > 0) {
        selectwrap.bind('click', function(event) {
            var target = $(event.target);
            if (target.is('a')) {
                var attr_value = selectwrap.attr('attr_value'); //全局值
                var valueArr = attr_value.split('.');
                var attr_id = target.attr('attr_id');
                var parentDl = $('#attr_id_' + attr_id);
                var index = selectwrap.children('dl').index(parentDl); //坐标
                var filters = target.attr('filterss');
                valueArr[index] = filters; //更新全局值
                var key = target.attr('ecvalue');
                var value = valueArr.join('.');
                replaceParam(key, value);
            }
        });
    }
    //导航属性删除
    location.bind('click', function(event) {
        var target = $(event.target);
        if (target.is('a') && target.parent().attr('ectype') == 'li_filter') {
            var ecvalue = target.attr('ecvalue');
            if (ecvalue === 'attr_value') {
                var attr_value = selectwrap.attr('attr_value'); //全局值
                var valueArr = attr_value.split('.');
                var filters = target.attr('filterss');
                var i, len, value;
                var key = target.attr('ecvalue');
                for (i = 0, len = valueArr.length; i < len; i++) {
                    if (valueArr[i] == filters) {
                        valueArr[i] = 0;
                        break;
                    }
                }
                value = valueArr.join('.');
                replaceParam(key, value);
            } else if (ecvalue === 'keyword') {
                var key = 'keyword';
                dropParam(key);
            } else if (ecvalue === 'price') {
                var key = 'price';
                dropParam(key);
            } else if (ecvalue === 'start_stock') {
                var key = 'start_stock';
                dropParam(key);
            }
        }
    });
    //关键字查询
    $('#location_input').keydown(function(event) {
        var keycode = event.keyCode;
        if (keycode === 13) {
            var value = $(this).val();
            var key = 'keyword';
            replaceParam(key, value);
        }
    });
    //排序查询
    var list_pltl = $('#list-pltl');
    list_pltl.bind('click', function(event) {
        var target = $(event.target);
        if (target.is('a')) {
            var key = target.attr('ectype');
            var value = target.attr('orders');
            if (value) {
                replaceParam(key, value);
            } else {
                dropParam(key);
            }
        } else if (target.is('i')) {
            var key = target.parent().attr('ectype');
            var value = target.parent().attr('orders');
            if (value) {
                replaceParam(key, value);
            } else {
                dropParam(key);
            }
        }
    });

});